package com.zkny.zknyerp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkny.zknyerp.mapper.ModelMapper;
import com.zkny.zknyerp.model.Model;
import com.zkny.zknyerp.service.ModelService;
import com.zkny.zknyerp.utils.Constant;
import com.zkny.zknyerp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 用户服务接口实现类
 */
@Service
@Transactional
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelMapper modelMapper;

    /**
     * 型号添加接口
     * @param model
     * @return
     */
    public Result addModel(String model){
        Example example = new Example(Model.class);
        example.createCriteria().andEqualTo("model",model);
        Model model1 = modelMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(model1)){
            return new Result(Constant.code_1017,Constant.msg_1017);
        }
        Model model2 = new Model();
        model2.setModel(model);
        int i = modelMapper.insert(model2);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 型号查询接口
     */
    public Result selectModel(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Model> list = modelMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        PageInfo<Model> pageInfo = new PageInfo<Model>(list);
        return new Result(Constant.code_0000,Constant.msg_0000,pageInfo);
    }

    /**
     * 输入框选项查询
     */

    public Result getModel(){
        List<Model> list = modelMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(Constant.code_1002,Constant.msg_1002);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,list);
    }

    /**
     * 型号数据修改接口
     */
    public Result updateModel(Model model){
        int i = modelMapper.updateByPrimaryKeySelective(model);
        if(0 == i ){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 型号删除接口
     */
    public Result deleteModel(Model model){
        int i = modelMapper.deleteByPrimaryKey(model);
        if(0 == i){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000);
    }

    /**
     * 根据id查询数据
     */
    public Result getId(Integer id){
        Example example = new Example(Model.class);
        example.createCriteria().andEqualTo("id",id);
        Model model = modelMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(model)){
            return new Result(Constant.code_9999,Constant.msg_9999);
        }
        return new Result(Constant.code_0000,Constant.msg_0000,model);
    }


}
